// Loggr::Tools::TimeCounter
// Tools make Loggr be more autonomous.
// By George Ulyanov (c) 2010

#include "Config.h"

#ifndef LOGGR_TOOLS_TIMECOUNTER_H_
#   include "TimeCounter.h"
#endif

#if defined (LOGGR_WIN32API)
#	include <Windows.h>
#elif defined (LOGGR_OTHER)
#	include <stdlib.h>
#endif
#include <stdexcept>

namespace Loggr
{
    namespace Tools
    {
        struct TimeCounterData
        {
#if defined (LOGGR_WIN32API)
            LARGE_INTEGER   m_startTime;
            LARGE_INTEGER   m_currentTime;
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        };

        TimeCounter::TimeCounter()
        {
            m_data = new TimeCounterData();
            if (!m_data)
            {
                throw std::runtime_error("Couldn't initialize TimeCounter::TimeCounterData.");
            }
            Reset();
        }

        TimeCounter::~TimeCounter() throw()
        {
            delete m_data;
        }

        void TimeCounter::Reset()
        {
#if defined (LOGGR_WIN32API)
            ::QueryPerformanceCounter(&m_data->m_startTime);
            ::QueryPerformanceCounter(&m_data->m_currentTime);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        }

        unsigned long TimeCounter::GetPeriod()
        {
#if defined (LOGGR_WIN32API)
            LARGE_INTEGER frequency;
            ::QueryPerformanceFrequency(&frequency);
            ::QueryPerformanceCounter(&m_data->m_currentTime);
            return static_cast< unsigned long >(1000000.0f * (m_data->m_currentTime.QuadPart - m_data->m_startTime.QuadPart) / frequency.QuadPart);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
            return 0;
#endif
        }
    }
    // namespace Tools
}
// namespace Loggr
